package com.laker.admin.module.datasource.controller;

import com.laker.admin.framework.aop.Metrics;
import com.laker.admin.framework.model.Response;
import com.laker.admin.module.datasource.entity.DataSourceCmd;
import com.laker.admin.module.datasource.facade.DataSourceFacade;
import com.laker.admin.module.excel.domain.DataSource;
import com.laker.admin.module.excel.entity.ExcelFillCmd;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 数据源
 * </p>
 *
 * @author nihongxin
 * @since 2021-12-08
 */
@RestController
@RequestMapping("/dataSource")
@Metrics
public class DataSourceController {

    @Autowired
    private DataSourceFacade dataSourceFacade;

    @PostMapping("/checkConnection")
    @ApiOperation(value = "检测数据库连通性（即jdbc的连通性）")
    public Response checkConnection(@RequestBody DataSourceCmd dataSourceCmd) {
        return dataSourceFacade.checkConnection(dataSourceCmd);
    }

    @PostMapping("/getDataBaseList")
    @ApiOperation(value = "获取所有库")
    public Response getDataBaseList(@RequestBody DataSourceCmd dataSourceCmd) {
        return dataSourceFacade.getDataBaseList(dataSourceCmd);
    }

    @PostMapping("/getTableList")
    @ApiOperation(value = "获取库下的所有表")
    public Response getTableList(@RequestBody DataSourceCmd dataSourceCmd) {
        return dataSourceFacade.getTableList(dataSourceCmd);
    }

    @PostMapping("/getTableDetail")
    @ApiOperation(value = "查询指定库、指定表的表字段信息")
    public Response getTableDetail(@RequestBody DataSourceCmd dataSourceCmd) {
        return dataSourceFacade.getTableDetail(dataSourceCmd);
    }

    @PostMapping("/getTableData")
    @ApiOperation(value = "查询指定库、指定表的数据")
    public Response getTableData(@RequestBody DataSourceCmd dataSourceCmd) {
        return dataSourceFacade.getTableData(dataSourceCmd);
    }

    @PostMapping("/updateTableData")
    @ApiOperation(value = "更新数据")
    public Response updateTableData(@RequestBody DataSourceCmd dataSourceCmd) {
        return dataSourceFacade.updateTableData(dataSourceCmd);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存数据", tags = "数据源")
    public Response save(@RequestBody DataSource dataSource) {
        return dataSourceFacade.save(dataSource);
    }

    @ApiOperation(value = "查询模版列表", tags = "数据源")
    @PostMapping(value = "/list")
    public Response list(@RequestBody ExcelFillCmd excelFillCmd) {
        return dataSourceFacade.list(excelFillCmd);
    }

    @ApiOperation(value = "获取单个数据", tags = "数据源")
    @GetMapping(value = "/getById/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", dataType = "Long", required = true, value = "主键id", paramType = "form")
    })
    public Response getById(@PathVariable Long id) {
        return dataSourceFacade.getById(id);
    }

    @ApiOperation(value = "删除数据", tags = "数据源")
    @DeleteMapping(value = "/deleteById/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", dataType = "Long", required = true, value = "主键id", paramType = "form")
    })
    public Response deleteById(@PathVariable Long id) {
        return dataSourceFacade.deleteById(id);
    }
}